package com.xzq.serializer;

import com.alibaba.fastjson.JSONObject;

import java.nio.charset.StandardCharsets;

/**
 * @Author xzq
 * @Description //TODO
 * @Date 2021/11/22 12:50
 * @Version 1.0.0
 **/
public class JSONSerializer implements Serializer {
    @Override
    public <T> byte[] serializer(T object) {
        String s = JSONObject.toJSONString(object);
        byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
        return bytes;
    }

    @Override
    public <T> T derializer(byte[] bytes, Class<T> clazz) {
        String string = new String(bytes, StandardCharsets.UTF_8);
        T t = JSONObject.parseObject(string, clazz);
        return t;
    }
}
